package com.foreknow.controller;

import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;
import com.foreknow.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        HttpSession session = req.getSession();
        //如何获取到请求的参数 email=tom&password=123456
        String username = req.getParameter("username"); //tom
        String password = req.getParameter("password"); //123456
        //调用Service
        AdminService service = new AdminServiceImpl();
        Admin admin = service.isValidate(username,password);
        if(admin!=null){
            //将用户登录的名字保存到服务器端
            session.setAttribute("admin",admin);
//            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
            resp.sendRedirect(req.getContextPath()+"/user/manager?q=list");
        }else {
            out.println("登陆失败");
        }


    }
}
